package com.yangguang.huahui.ui.fragments;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.aigestudio.wheelpicker.core.AbstractWheelPicker;
import com.aigestudio.wheelpicker.widget.cross.CrossDatePicker;
import com.yangguang.huahui.Config;
import com.yangguang.huahui.R;
import com.yangguang.huahui.entity.BuyEditInfo;
import com.yangguang.huahui.entity.DataInfo;
import com.yangguang.huahui.entity.HttpResult;
import com.yangguang.huahui.entity.Upload;
import com.yangguang.huahui.mvp.presenter.PublishBuyPresenter;
import com.yangguang.huahui.mvp.view.PublishView;
import com.yangguang.huahui.ui.activitys.BaseActivity;
import com.yangguang.huahui.ui.activitys.InputActivity;
import com.yangguang.huahui.ui.adapters.OnItemClickListener;
import com.yangguang.huahui.ui.adapters.PinzhongAdapter;
import com.yangguang.huahui.ui.adapters.ProvinceAdapter;
import com.yangguang.huahui.utils.CommonUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/3/8.
 */

public class PublishBuyFragment extends BaseMvpFragment<PublishView, PublishBuyPresenter> implements PublishView {
    BaseActivity baseActivity;
    @BindView(R.id.tv_goods_type)
    TextView tv_goods_type;//商品分类
    @BindView(R.id.tv_goods_name)
    TextView tv_goods_name;
    @BindView(R.id.mLL_hide)
    LinearLayout mLL_hide;
    @BindView(R.id.tv_source)
    TextView tv_source;
    @BindView(R.id.tv_variety)
    TextView tv_variety;
    @BindView(R.id.tv_high_price)
    TextView tv_high_price;
    @BindView(R.id.tv_low_price)
    TextView tv_low_price;
    @BindView(R.id.tv_demand)
    TextView tv_demand;
    @BindView(R.id.tv_date)
    TextView tv_date;
    @BindView(R.id.tv_contact)
    TextView tv_contact;
    @BindView(R.id.tv_phone)
    TextView tv_phone;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.btn_publish)
    Button btn_publish;

    String goods_type = "";
    String variety = "";
    String deadline = "";

    private int type;

    @OnClick({R.id.mLL_goods_type, R.id.mLL_source, R.id.tv_high_price, R.id.tv_low_price, R.id.tv_demand,
            R.id.mLL_demand, R.id.mLL_address, R.id.mLL_phone, R.id.mLL_contact, R.id.mLL_date, R.id.btn_publish})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.mLL_goods_type:
                selectPinlei("商品分类");
                break;
            case R.id.mLL_source:
                selectAddress("期望货源地");
                break;
            case R.id.tv_low_price:
                startActivityForResult(new Intent(mActivity, InputActivity.class)
                        .putExtra("input_content", tv_low_price.getText().toString())
                        .putExtra("input_title", "最低价")
                        .putExtra("input_length", 4)
                        .putExtra("edit_type", InputType.TYPE_CLASS_NUMBER)
                        .putExtra("input_type", 3), Config.INPUT_REQUEST_CODE);
                break;
            case R.id.tv_high_price:
                startActivityForResult(new Intent(mActivity, InputActivity.class)
                        .putExtra("input_content", tv_high_price.getText().toString())
                        .putExtra("input_title", "最低价")
                        .putExtra("input_length", 4)
                        .putExtra("edit_type", InputType.TYPE_CLASS_NUMBER)
                        .putExtra("input_type", 4), Config.INPUT_REQUEST_CODE);
                break;
            case R.id.mLL_demand:
                startActivityForResult(new Intent(mActivity, InputActivity.class)
                        .putExtra("input_content", tv_demand.getText().toString())
                        .putExtra("input_title", "需求量")
                        .putExtra("input_length", 4)
                        .putExtra("edit_type", InputType.TYPE_CLASS_NUMBER)
                        .putExtra("input_type", 5), Config.INPUT_REQUEST_CODE);
                break;
            case R.id.mLL_date:
                dateAllDialog("采购截至时间");
                break;
            case R.id.mLL_address:
                startActivityForResult(new Intent(mActivity, InputActivity.class)
                        .putExtra("input_content", tv_address.getText().toString())
                        .putExtra("input_title", "收货地址")
                        .putExtra("input_length", 15)
                        .putExtra("input_type", 8), Config.INPUT_REQUEST_CODE);
                break;
            case R.id.mLL_contact:
                startActivityForResult(new Intent(mActivity, InputActivity.class)
                        .putExtra("input_content", tv_contact.getText().toString())
                        .putExtra("input_title", "联系人")
                        .putExtra("input_length", 10)
                        .putExtra("input_type", 9), Config.INPUT_REQUEST_CODE);
                break;
            case R.id.mLL_phone:
                startActivityForResult(new Intent(mActivity, InputActivity.class)
                        .putExtra("input_content", tv_phone.getText().toString())
                        .putExtra("input_title", "联系电话")
                        .putExtra("edit_type", 101)
                        .putExtra("input_type", 10), Config.INPUT_REQUEST_CODE);
                break;
            case R.id.btn_publish:
                submit();
                break;
        }
    }

    @Override
    protected int initLayoutId() {
        return R.layout.frag_publish_buy;
    }

    @Override
    protected void initView() {
        baseActivity = (BaseActivity) getActivity();
        baseActivity.getArea();
        baseActivity.getFlowers_type();
        type = getArguments().getInt("type");
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onSuccess(BuyEditInfo buyEditInfo) {
        btn_publish.setVisibility(View.GONE);
        mLL_hide.setVisibility(View.VISIBLE);
        tv_goods_type.setText(buyEditInfo.getInfo().getGoods_type());
        tv_variety.setText(buyEditInfo.getInfo().getVariety());
        tv_goods_name.setText(buyEditInfo.getInfo().getGoods_title());
        tv_demand.setText(buyEditInfo.getInfo().getDemand());
        tv_low_price.setText(buyEditInfo.getInfo().getLow_price());
        tv_high_price.setText(buyEditInfo.getInfo().getHigh_price());
        tv_source.setText(buyEditInfo.getInfo().getSource_of_supply_name());
        if (!CommonUtil.isNull(buyEditInfo.getInfo().getDeadline())) {
            tv_date.setText(CommonUtil.longToTime(Long.parseLong(buyEditInfo.getInfo().getDeadline() + "000"), Calendar.DATE));
        }
        tv_address.setText(buyEditInfo.getInfo().getReceipt_address());
        tv_contact.setText(buyEditInfo.getInfo().getContacts());
        tv_phone.setText(buyEditInfo.getInfo().getPhone());
    }

    @Override
    public void onSuccess(HttpResult httpResult) {
        CommonUtil.myToastA(mActivity, httpResult.getMsg());
        if (httpResult.getStatus() == 200) {
            if (type == 2) return;
            baseActivity.finish();
        }
    }

    @Override
    public void onSuccess(Upload upload) {

    }

    @Override
    public void onError() {

    }

    @Override
    protected PublishBuyPresenter initPresenter() {
        return new PublishBuyPresenter();
    }

    @Override
    protected void fetchData() {
        if (type == 2){
            mPresenter.getEditData(getArguments().getString("id"));
        }
    }

    private int fistPosition = -1;
    private int seconPosition = -1;
    private int thirdPosition = -1;
    private String yiji_id = "";
    private String erji_id = "";
    private String erji_name = "";
    private String sanji_id = "";
    private String sanji_name = "";

    private void selectPinlei(String title) {
        View contentView = LayoutInflater.from(mActivity).inflate(R.layout.area_select, null);
        TextView tv_title = (TextView) contentView.findViewById(R.id.tv_input_title);
        tv_title.setText(title);
        ListView lv_first = (ListView) contentView.findViewById(R.id.lv_first);
        ListView lv_second = (ListView) contentView.findViewById(R.id.lv_second);
        final ListView lv_third = (ListView) contentView.findViewById(R.id.lv_third);
        PinzhongAdapter pinzhongAdapter = new PinzhongAdapter(mActivity, baseActivity.flowersType.getList(), R.layout.select_area_list_item,
                lv_second, lv_third, fistPosition, seconPosition, thirdPosition);
        lv_first.setAdapter(pinzhongAdapter);
        final AlertDialog alertDialog = baseActivity.show_dialog_max(contentView);
        pinzhongAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String id, String name) {
                yiji_id = id;
            }
        });
        PinzhongAdapter.SecondAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String id, String name) {
                erji_id = id;
                erji_name = name;
                sanji_id = "";
                sanji_name = "";
            }
        });
        PinzhongAdapter.SecondAdapter.ThirdAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String id, String name) {
                sanji_id = id;
                sanji_name = name;
            }
        });
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.iv_close:
                        alertDialog.dismiss();
                        break;
                    case R.id.iv_ok:
                        if (lv_third.getAdapter() != null
                                && CommonUtil.isNull(sanji_id)) {
                            CommonUtil.myToastA(mActivity, "请选择第三类");
                            return;
                        }

                        mLL_hide.setVisibility(View.VISIBLE);
                        tv_goods_type.setText(erji_name);
                        tv_variety.setText(sanji_name);
                        tv_goods_name.setText(sanji_name + " " + erji_name);
                        goods_type = erji_id;
                        variety = sanji_id;
                        if (type==2){
                            mPresenter.edit(getArguments().getString("id"), "goods_type", goods_type);
                            mPresenter.edit(getArguments().getString("id"), "variety", variety);
                            mPresenter.edit(getArguments().getString("id"), "goods_title", sanji_name+ " " + erji_name);
                        }
                        alertDialog.dismiss();
                        break;
                }
            }
        };
        contentView.findViewById(R.id.iv_close).setOnClickListener(listener);
        contentView.findViewById(R.id.iv_ok).setOnClickListener(listener);
    }

    private int proPosition = -1;
    private int cityPostion = -1;
    private int areaPosition = -1;
    String source_id = "";
    String source_name = "";

    private void selectAddress(String title) {
        final List<String> list_id = new ArrayList<>();
        final List<String> list_name = new ArrayList<>();
        source_id = "";
        source_name = "";
        View contentView = LayoutInflater.from(mActivity).inflate(R.layout.area_select, null);
        TextView tv_title = (TextView) contentView.findViewById(R.id.tv_input_title);
        tv_title.setText(title);
        ListView lv_first = (ListView) contentView.findViewById(R.id.lv_first);
        ListView lv_second = (ListView) contentView.findViewById(R.id.lv_second);
        ListView lv_third = (ListView) contentView.findViewById(R.id.lv_third);
        ProvinceAdapter provinceAdapter = new ProvinceAdapter(mActivity, baseActivity.mArea.getList(), R.layout.select_area_list_item,
                lv_second, lv_third, proPosition, cityPostion, areaPosition);
        provinceAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String id, String name) {
                fistPosition = position;
                if (!CommonUtil.isNull(id)) {
                    if (list_id.size() > 0) {
                        list_id.removeAll(list_id);
                    }
                    list_id.add(id);
                }
                if (!CommonUtil.isNull(name)) {
                    if (list_name.size() > 0) {
                        list_name.removeAll(list_name);
                    }
                    list_name.add(name);
                }
            }
        });
        ProvinceAdapter.CityAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String id, String name) {
                seconPosition = position;
                if (!CommonUtil.isNull(id)) {
                    if (list_id.size() == 2) {
                        list_id.remove(1);
                    } else if (list_id.size() == 3) {
                        list_id.remove(1);
                        list_id.remove(1);
                    }
                    list_id.add(id);
                }
                if (!CommonUtil.isNull(name)) {
                    if (list_name.size() == 2) {
                        list_name.remove(1);
                    } else if (list_name.size() == 3) {
                        list_name.remove(1);
                        list_name.remove(1);
                    }
                    list_name.add(name);
                }
            }
        });
        ProvinceAdapter.CityAdapter.AreaAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String id, String name) {
                thirdPosition = position;
                if (!CommonUtil.isNull(id)) {
                    if (list_id.size() == 3) {
                        list_id.remove(2);
                    }
                    list_id.add(id);
                }
                if (!CommonUtil.isNull(name)) {
                    if (list_name.size() == 3) {
                        list_name.remove(2);
                    }
                    list_name.add(name);
                }
            }
        });
        lv_first.setAdapter(provinceAdapter);
        baseActivity.show_dialog_max(contentView, mActivity);
        View.OnClickListener areaListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.iv_close:
                        baseActivity.alertDialog.dismiss();
                        break;
                    case R.id.iv_ok:
                        for (String name : list_name) {
                            source_name += name + "-";
                        }
                        if (source_name.length() > 0) {
                            source_name = source_name.substring(0, source_name.length() - 1);
                        }
                        tv_source.setText(source_name);
                        for (String name : list_id) {
                            source_id += name + ",";
                        }
                        if (source_id.length() > 0) {
                            source_id = source_id.substring(0, source_id.length() - 1);
                        }
                        if (type == 2){
                            mPresenter.edit(getArguments().getString("id"), "source_of_supply_id", source_id);
                        }
                        baseActivity.alertDialog.dismiss();
                        break;
                }
            }
        };
        contentView.findViewById(R.id.iv_close).setOnClickListener(areaListener);
        contentView.findViewById(R.id.iv_ok).setOnClickListener(areaListener);
    }

    private String select_date = "";

    private void dateAllDialog(String title) {
        final View contentView = LayoutInflater.from(mActivity).inflate(R.layout.dialog_date_all, null);
        final AlertDialog alertDialog = baseActivity.show_bottom_dialog(contentView);
        CrossDatePicker datePicker = (CrossDatePicker) contentView.findViewById(R.id.wheel_str_date);
        TextView tv_title = (TextView) contentView.findViewById(R.id.tv_input_title);
        tv_title.setText(title);
        datePicker.setOnWheelChangeListener(new AbstractWheelPicker.OnWheelChangeListener() {
            @Override
            public void onWheelScrolling(float deltaX, float deltaY) {

            }

            @Override
            public void onWheelSelected(int index, String data) {
                select_date = data;
            }

            @Override
            public void onWheelScrollStateChanged(int state) {

            }
        });
        View.OnClickListener date_listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.iv_ok:
                        long select_millis = CommonUtil.getMillisFromDate(select_date, "yyyy-MM-dd");
                        tv_date.setText(select_date);
                        deadline = select_millis + "";
                        if (type == 2){
                            mPresenter.edit(getArguments().getString("id"), "deadline", deadline);
                        }
                        alertDialog.dismiss();
                        break;
                    case R.id.iv_close:
                        alertDialog.dismiss();
                        break;
                }
            }
        };
        contentView.findViewById(R.id.iv_close).setOnClickListener(date_listener);
        contentView.findViewById(R.id.iv_ok).setOnClickListener(date_listener);
    }

    private void submit() {
        if (CommonUtil.isNull(tv_goods_name.getText().toString())) {
            CommonUtil.myToastA(mActivity, "采购货品不为空");
            return;
        }
        if (CommonUtil.isNull(variety)) {
            CommonUtil.myToastA(mActivity, "品种不为空");
            return;
        }
        if (CommonUtil.isNull(tv_low_price.getText().toString())) {
            CommonUtil.myToastA(mActivity, "最低价不为空");
            return;
        }
        if (CommonUtil.isNull(deadline)) {
            CommonUtil.myToastA(mActivity, "采购截止时间不为空");
            return;
        }
        mPresenter.publish(DataInfo.TOKEN, goods_type, variety, tv_goods_name.getText().toString(), tv_high_price.getText().toString(),
                tv_low_price.getText().toString(), tv_demand.getText().toString(), source_id, tv_address.getText().toString(),
                deadline, tv_contact.getText().toString(), tv_phone.getText().toString(), "");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.INPUT_REQUEST_CODE) {
            if (resultCode == Config.INPUT_RESULT_CODE) {
                switch (data.getIntExtra("input_type", 0)) {
                    case 1:
                        tv_goods_name.setText(data.getStringExtra("input"));
                        break;
                    case 3:
                        if (!CommonUtil.isNull(tv_high_price.getText().toString())) {
                            if (Integer.valueOf(data.getStringExtra("input")) < Integer.valueOf(tv_high_price.getText().toString())) {
                                tv_low_price.setText(data.getStringExtra("input"));
                                if (type == 2){
                                    mPresenter.edit(getArguments().getString("id"), "low_price", data.getStringExtra("input"));
                                }
                            } else {
                                CommonUtil.myToastA(mActivity, "最低价需小于最高价");
                            }
                        } else {
                            tv_low_price.setText(data.getStringExtra("input"));
                            if (type == 2){
                                mPresenter.edit(getArguments().getString("id"), "low_price", data.getStringExtra("input"));
                            }
                        }
                        break;
                    case 4:
                        if (!CommonUtil.isNull(tv_low_price.getText().toString())) {
                            if (Integer.valueOf(data.getStringExtra("input")) > Integer.valueOf(tv_low_price.getText().toString())) {
                                tv_high_price.setText(data.getStringExtra("input"));
                                if (type == 2){
                                    mPresenter.edit(getArguments().getString("id"), "high_price", data.getStringExtra("input"));
                                }
                            } else {
                                CommonUtil.myToastA(mActivity, "最高价需大于最低价");
                            }
                        } else {
                            tv_high_price.setText(data.getStringExtra("input"));
                            if (type == 2){
                                mPresenter.edit(getArguments().getString("id"), "high_price", data.getStringExtra("input"));
                            }
                        }
                        break;
                    case 5:
                        if (!CommonUtil.isNull(data.getStringExtra("input")) && Integer.valueOf(data.getStringExtra("input")) > 0) {
                            tv_demand.setText(data.getStringExtra("input"));
                            if (type == 2){
                                mPresenter.edit(getArguments().getString("id"), "demand", data.getStringExtra("input"));
                            }
                        } else {
                            CommonUtil.myToastA(mActivity, "最小需求量为1");
                        }
                        break;
                    case 8:
                        tv_address.setText(data.getStringExtra("input"));
                        if (type == 2){
                            mPresenter.edit(getArguments().getString("id"), "receipt_address", data.getStringExtra("input"));
                        }
                        break;
                    case 9:
                        tv_contact.setText(data.getStringExtra("input"));
                        if (type == 2){
                            mPresenter.edit(getArguments().getString("id"), "contacts", data.getStringExtra("input"));
                        }
                        break;
                    case 10:
                        tv_phone.setText(data.getStringExtra("input"));
                        if (type == 2){
                            mPresenter.edit(getArguments().getString("id"), "phone", data.getStringExtra("input"));
                        }
                        break;
                }
            }
        }
    }
}
